﻿using System;
using System.Collections.Generic;
using System.IO;
using Windows.UI.Xaml.Media.Imaging;
using System.Linq;
using System.Windows.Input;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.Input;
using Windows.UI.Core;
using System.Threading;
using System.Collections.Concurrent;

namespace RoboRun
{
    class TouchDevelopCode
    {
        public static List<String> TDQueue = new List<string>();
        private static bool withinLoop = false;
        private static char comparativeOperator;

        private static char getRow(string position)
        {
            char row = position.ToCharArray()[0];
            return row;
        }

        private static char getColumn(string position)
        {
            char column = position.ToCharArray()[2];
            return column;
        }


        public static void addGridDetails()
        {
            char gridSize = ' ';
            char robotDirection = ' ';
            char robotRow;
            char robotColumn;
            char goalRow;
            char goalColumn;
            switch (GameplayScreen.GridType)
            {
                case "3x3":
                    gridSize = '3';
                    break;
                case "4x4":
                    gridSize = '4';
                    break;
            }

            switch (GameplayScreen.RobotStartingDirection)
            {
                case "North":
                    robotDirection = '0';
                    break;
                case "East":
                    robotDirection = '1';
                    break;
                case "South":
                    robotDirection = '2';
                    break;
                case "West":
                    robotDirection = '3';
                    break;
            }

            robotRow = getRow(GameplayScreen.RobotStartingPosition);
            robotColumn = getColumn(GameplayScreen.RobotStartingPosition);
            goalRow = getRow(GameplayScreen.GoalPosition);
            goalColumn = getColumn(GameplayScreen.GoalPosition);

            TDQueue.Add("robot → initialise(" + gridSize + ", " + robotDirection + ", " + robotRow + ", " + robotColumn + ", " + goalRow + ", " + goalColumn + ")");
        }

        public static void addObstaclePositions()
        {
            foreach (string obstacle in GameplayScreen.ObstaclePositions)
            {
                TDQueue.Add("robot → create obstacle(" + getRow(obstacle) + ", " + getColumn(obstacle) + ")");
            }
        }

        public static void parseCommandQueue()
        {
            int count = 0;
            count++;
            string output = count.ToString();
            foreach (String command in GameplayScreen.commandQueue)
            {
                if (command.Contains("Straight"))
                {
                    int index = command.IndexOf("Straight");
                    index += 9;
                    char[] commandArray = command.ToCharArray();
                    char squares = commandArray[index];
                    if (command.Contains("\t") || command.Contains("   "))
                    {
                        TDQueue.Add("\t robot → Go Straight (" + squares + ")");
                    }
                    else
                    {
                        TDQueue.Add("robot → Go Straight (" + squares + ")");
                    }
                }
                else if (command.Contains("Anti-Clockwise"))
                {
                    if (command.Contains("\t") || command.Contains("   "))
                    {
                        TDQueue.Add("\trobot → rotate anticlockwise");
                    }
                    else
                    {
                        TDQueue.Add("robot → rotate anticlockwise");
                    }
                    
                }
                else if (command.Contains("Clockwise"))
                {
                    if (command.Contains("\t") || command.Contains("   "))
                    {
                        TDQueue.Add("\trobot → rotate clockwise");
                    }
                    else
                    {
                        TDQueue.Add("robot → rotate clockwise");
                    }
                    
                }
                else if (command.Contains("For"))
                {
                    char[] commandArray = command.ToCharArray();
                    char numberOfIterations = commandArray[7];
                    TDQueue.Add("For 0 ≤ i  < " + numberOfIterations + " do");
                }
                else if (command.Contains("[") || command.Contains("]"))
                {
                    continue;
                }
                else if (command.Contains("If"))
                {
                    
                    if (command.Contains("Greater") && command.Contains("equal to"))
                    {
                        comparativeOperator = '≥';
                        int index = command.IndexOf("to");
                        string squareAway = command.Substring(index + 3, 1);
                        TDQueue.Add("if detect squares away() " + comparativeOperator + " " + squareAway + " then");
                        
                    }
                    else if (command.Contains("Greater"))
                    {
                        comparativeOperator = '>';
                        int index = command.IndexOf("than");
                        string squareAway = command.Substring(index + 5, 1);
                        TDQueue.Add("if detect squares away() " + comparativeOperator + " "+ squareAway + " then");
                    }
                    else if (command.Contains("Exactly"))
                    {
                        comparativeOperator = '=';
                        int index = command.IndexOf("Exactly");
                        string squareAway = command.Substring(index + 8, 1);
                        TDQueue.Add("if detect squares away() " + comparativeOperator + " " + squareAway + " then");
                    }
                    else if (command.Contains("Less than") && command.Contains("equal to"))
                    {
                        comparativeOperator = '≤';
                        int index = command.IndexOf("to");
                        string squareAway = command.Substring(index + 3, 1);
                        TDQueue.Add("if detect squares away() " + comparativeOperator + " " + squareAway + " then");
                    }
                    else
                    {
                        comparativeOperator = '<';
                        int index = command.IndexOf("than");
                        string squareAway = command.Substring(index + 5, 1);
                        TDQueue.Add("if detect squares away() " + comparativeOperator + " " + squareAway + " then");
                    }

                }
                else if (command.Contains("Else"))
                {
                    TDQueue.Add("else");
                }
                else if (command.Contains("While"))
                {
                    if (command.Contains("Greater") && command.Contains("equal to"))
                    {
                        char comparativeOperator = '≥';
                        int index = command.IndexOf("to");
                        string squareAway = command.Substring(index + 3, 1);
                        TDQueue.Add("while detect squares away() " + comparativeOperator + " " + squareAway + " do");
                    }
                    else if (command.Contains("Greater"))
                    {
                        comparativeOperator = '>';
                        int index = command.IndexOf("than");
                        string squareAway = command.Substring(index + 5, 1);
                        TDQueue.Add("while detect squares away() " + comparativeOperator + " " + squareAway + " do");
                    }
                    else if (command.Contains("Exactly"))
                    {
                        comparativeOperator = '=';
                        int index = command.IndexOf("Exactly");
                        string squareAway = command.Substring(index + 8, 1);
                        TDQueue.Add("while detect squares away() " + comparativeOperator + " " + squareAway + " do");
                    }
                    else if (command.Contains("Less than") && command.Contains("equals to"))
                    {
                        comparativeOperator = '≤';
                        int index = command.IndexOf("to");
                        string squareAway = command.Substring(index + 3, 1);
                        TDQueue.Add("while detect squares away() " + comparativeOperator + " " + squareAway + " do");
                    }
                    else
                    {
                        comparativeOperator = '<';
                        int index = command.IndexOf("than");
                        string squareAway = command.Substring(index + 5, 1);
                        TDQueue.Add("while detect squares away() " + comparativeOperator + " " + squareAway + " do");
                    }
                }
            }
        }

        public static string outputCode()
        {
            addGridDetails();
            addObstaclePositions();
            parseCommandQueue();
            return String.Join("\n", TDQueue);
            
        }

    }
}
